<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>01 - OL4加载天地图WMTS服务展示地图</title>
    <script src="../lib/ol4/ol4.js"></script>
    <script src="../lib/proj4/proj4.js"></script>
    <link rel="stylesheet" href="../css/ol4/ol.css">
    <link rel="stylesheet" href="../css/main.css">
</head>
<body>
<div id="map"></div>
</body>
<script>
    const projection = ol.proj.get('EPSG:4326');
    console.log(projection);
    // projection.setExtent([113.97190624843674, 22.587473988906922, 114.00529456775023, 22.60613855004298]);
    const tileSize = 256;
    const projectionExtent = projection.getExtent();
    const origins = [
        [-180.0, 90.0],
        ol.extent.getTopLeft(projectionExtent)
    ];
    const origin = origins[0];
    console.log('extent:', projectionExtent);
    console.log('origin:', origin);

    const size = ol.extent.getWidth(projectionExtent) / tileSize;

    const resolutions = [1.25764139776733, 0.628820698883665, 0.251528279553466, 0.125764139776733, 0.0628820698883665, 0.0251528279553466, 0.0125764139776733, 0.00628820698883665, 0.00251528279553466, 0.00125764139776733, 6.28820698883665E-4, 2.51528279553466E-4, 1.25764139776733E-4, 6.28820698883665E-5, 2.51528279553466E-5, 1.25764139776733E-5, 6.28820698883665E-6, 2.51528279553466E-6, 1.25764139776733E-6, 6.28820698883665E-7, 2.51528279553466E-7];
    console.log(resolutions);
    const matrixIds = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

    const tileGrid = new ol.tilegrid.WMTS({
        origin,
        resolutions: resolutions,
        extent: projectionExtent,
        matrixIds: matrixIds
    });

    // const url = `http://t0.tianditu.gov.cn/vec_c/wmts?tk=a6bf5a2496115f58481b947040c83ae3`;
    // const url = `https://ditu.zjzwfw.gov.cn/services/wmts/emap/default/oss?tk=b5b21163b68ca5628ef3bc0afe9105d0`;
    // const url = `https://ditu.zjzwfw.gov.cn:443/services/wmts/emap_lab/default/oss?tk=b5b21163b68ca5628ef3bc0afe9105d0`;
    // const url = `http://t0.tianditu.gov.cn/img_c/wmts?tk=695f6f53c1c0087ed8ab5b52f7c446bb`;
    const url = `http://gis.szpt.edu.cn/iserver/services/map-ugcv5-szymap/wmts100`;

    const wmtsSource = new ol.source.WMTS({
        url,
        layer: 'szymap',
        matrixSet: 'GlobalCRS84Scale_szymap',
        format: 'image/png',
        projection: projection,
        tileGrid: tileGrid,
        style: 'default',
        wrapX: false
    });
    // 图层
    const baseLayer = new ol.layer.Tile({
        source: wmtsSource
    });

    // const circlePoint = ol.proj.transform([116.384722, 39.889916], 'EPSG:4326', 'EPSG:4490');
    const points = [
        [113.98860040809348,22.596806269474953]
    ];
    const centerPoint = points[0];

    const iconFeature = new ol.Feature({
        geometry: new ol.geom.Point(centerPoint)
    });

    const iconStyle = new ol.style.Style({
        image: new ol.style.Icon({
            anchor: [10, 26],
            anchorXUnits: 'pixels',
            anchorYUnits: 'pixels',
            src: '../image/position_orange.png'
        })
    });

    iconFeature.setStyle(iconStyle);

    const vectorSource = new ol.source.Vector({
        features: [iconFeature],
        wrapX: false
    });

    const iconLayer = new ol.layer.Vector({
        source: vectorSource
    });

    const map = new ol.Map({
        layers: [
            baseLayer,
            iconLayer
        ],
        target: 'map',
        view: new ol.View({
            center: centerPoint,
            projection,
            zoom: 5,
            maxZoom: 21,
            minZoom: 1,
            resolutions
        })
    });

    // const metersPerUnit = map.getView().getProjection().getMetersPerUnit();
    //
    // const realRadius = radius / metersPerUnit;
    //
    // const circleFeature = new ol.Feature({
    //     geometry: new ol.geom.Circle(centerPoint, realRadius)
    // });

</script>
</html>